﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace program1
{
    public class SchoolService
    {
        private SchoolRepository schoolRepository;
        private LogRepository logRepository;

        public SchoolService(string connectionString)
        {
            schoolRepository = new SchoolRepository(connectionString);
            logRepository = new LogRepository(connectionString);
        }

        public void AddSchool(string name, string location)
        {
            try
            {
                // 在此方法中，你可以实现添加学校的业务逻辑
                schoolRepository.AddSchool(name, location);

                // 记录日志
                logRepository.LogAction("Add School", "Schools", "Added a new school with name: " + name);
            }
            catch (Exception ex)
            {
                // 处理异常或记录错误日志
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void DeleteSchool(int schoolID)
        {
            try
            {
                // 在此方法中，你可以实现删除学校的业务逻辑
                School school = schoolRepository.GetSchoolById(schoolID);

                if (school != null)
                {
                    schoolRepository.DeleteSchool(schoolID);

                    // 记录日志
                    logRepository.LogAction("Delete School", "Schools", "Deleted school with ID: " + schoolID + ", Name: " + school.Name);
                }
            }
            catch (Exception ex)
            {
                // 处理异常或记录错误日志
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void UpdateSchool(int schoolID, string newName, string newLocation)
        {
            try
            {
                // 在此方法中，你可以实现更新学校信息的业务逻辑
                School school = schoolRepository.GetSchoolById(schoolID);

                if (school != null)
                {
                    schoolRepository.UpdateSchool(schoolID, newName, newLocation);

                    // 记录日志
                    logRepository.LogAction("Update School", "Schools", $"Updated school with ID: {schoolID}, Name: {school.Name} -> {newName}, Location: {school.Location} -> {newLocation}");
                }
            }
            catch (Exception ex)
            {
                // 处理异常或记录错误日志
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public List<School> GetSchools()
        {
            try
            {
                // 在此方法中，你可以实现获取学校信息的业务逻辑
                return schoolRepository.GetAllSchools();
            }
            catch (Exception ex)
            {
                // 记录错误日志或其他错误处理
                Console.WriteLine("Error: " + ex.Message);
                throw; // 继续抛出异常以供调用方处理
            }
        }

        public List<School> LoadSchools()
        {
            return schoolRepository.GetAllSchools();
        }



    }
}
